package com.tulele.vtool.task.async;

import com.alibaba.fastjson.JSONObject;
import com.tulele.vtool.bus.model.UserInfo;
import com.tulele.vtool.bus.model.thirdparty.WxPushUserInfo;
import com.tulele.vtool.bus.service.IUserBrandService;
import com.tulele.vtool.bus.service.IUserService;
import com.tulele.vtool.bus.service.SysGzhService;
import com.tulele.vtool.model.PlatformSysInfo;
import com.tulele.vtool.util.RedisCacheUtil;
import com.tulele.vtool.util.SpringContextUtil;
import com.tulele.vtool.wechat.service.WeixinService;
import com.tulele.vtool.wechat.service.WeixinServiceDL;
import com.tulele.vtool.wechat.service.WeixinServiceMap;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Optional;
import java.util.Set;

@Slf4j
@Component
public class UserDailyBrandSetInfoPushAsyncTask {


    @Autowired
    private ObjectFactory<WeixinServiceDL> objectFactory;

    @Autowired
    private SysGzhService sysGzhService;

    @Autowired
    private IUserBrandService userBrandService;

//    @Value("${setBrandTempID}")
//    private String setBrandTempID;
//
//    @Value("${frontDomain}")
//    private String frontDomain;

    //20200405 hyl 改成同步发 因为多线程 数据库顶不住
    //20200512 调整参数，多线程跑
    //@Async("taskExecutor")
    public void execute(UserInfo user,String profile) {

        //获取是否已经设置了品牌
        int userSubBrandCount = Optional.ofNullable(userBrandService.getUserSubBrandCount(user.getUserId())).orElse(0);
        if (userSubBrandCount == 0) {
            PlatformSysInfo sysGzh = sysGzhService.getPlatformSysInfo(profile);
            // 发送模板消息接口
            WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                    // 接收者openid
                    .toUser(user.getOpenId())
                    // 模板id
                    .templateId(sysGzh.getSysGzh().getSetBrandTempId())
                    // 模板跳转链接
                    .url(sysGzh.getFrontDomain() + "/pages/article/editBrands.html")
                    .build();

            String firstMsg = "为不影响您的产品体验，请尽快选择您关注的品牌。";
            String remarkMsg = "\uD83D\uDC49回复数字【3】马上设置";

            // 添加模板数据
            templateMessage.addData(new WxMpTemplateData("first", firstMsg, "#FF00FF"))
                    .addData(new WxMpTemplateData("keyword1", user.getNickName(), "#A9A9A9"))
                    .addData(new WxMpTemplateData("keyword2", "未设置关注品牌", "#FF0000"))
                    .addData(new WxMpTemplateData("remark", remarkMsg, "#0000FF"));
            try {
                WeixinServiceDL weixinService= WeixinServiceMap.getWeixinService(profile);
                if(weixinService==null) {
                    weixinService = objectFactory.getObject();
                    weixinService.reSetConfigStorage(profile);
                    WeixinServiceMap.putWeixinService(profile,weixinService);
                }
                weixinService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            } catch (WxErrorException e) {
                e.printStackTrace();
            }
        }
    }
}
